var baseConfig = require("../../baseConfig")

Page({
  data: {
    cdkey: null,
    qrCodeLayer: false
  },
  onLoad: function (options) {

  },
  contactMeClick() {
    this.setData({
      qrCodeLayer: true
    })
  },
  closeClick() {
    this.setData({
      qrCodeLayer: false
    })
  },
  saveToAlbum(filePath) {
    wx.saveImageToPhotosAlbum({
      filePath: "/static/yuanxudong.jpg",
      success: (res) => {
        console.log(res)
        this.showToast('保存成功')
      },
      fail: (err) => {
        console.log(err)
        this.showToast('保存失败')
      }
    })
  },
  copyWeChatNum() {
    wx.setClipboardData({
      data: "logic-yuan",
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            console.log(res.data)
          }
        })
      }
    })

  },
  bindNumber: function () {
    var e = this;
    if (e.data.cdkey) {
      wx.getStorage({
        key: 'userid',
        success: function (res) {
          wx.request({
            url: baseConfig.baseUrl + "users/bindCDKEY",
            method: "POST",
            data: {
              cdkey: e.data.cdkey,
            },
            header: {
              "Content-Type": "application/json",
              "UID": res.data
            },
            success: function (n) {
              console.log(n.data.data);
              if (200 === n.data.status) {
                e.showMsg("会员已激活");
                let userInfo = wx.getStorageSync("userInfo");
                userInfo.role = n.data.data.role;
                // console.log(userInfo);
                wx.setStorageSync("userInfo", userInfo);
                setTimeout(() => {
                  wx.switchTab({
                    url: '/pages/me/me',
                    success: function () {
                      var page = getCurrentPages().pop();
                      if (page == undefined || page == null) return;
                      page.onLoad(); //重新刷新页面
                    }
                  })
                }, 1500)

              } else {
                e.showMsg(n.data.message);
              }
            },
            fail: err => {
              console.log(err);
            }
          })
        },
        fail: err => {
          this.showMsg("请重新输入激活码！");
        }
      })
    } else this.showMsg("请填写激活码！");
  },
  showMsg: function (n) {
    wx.showToast({
      title: n,
      icon: "none",
      // duration: 3000
    });
  },
  changeCdk: function (n) {
    this.setData({
      cdkey: n.detail.value
    })
  }
})